﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wpftest.CommonClass;

namespace Wpftest.FormFile
{
    /// <summary>
    /// DataBindingForm.xaml 的交互逻辑
    /// </summary>
    /// 
    //绑定的基本要素：绑定源，绑定目标，绑定路径，绑定方向
    public partial class DataBindingForm : Window
    {
        private Student stu;
        public DataBindingForm()
        {
            InitializeComponent();
            this.stu = new Student { Name = "张三", Age = 10, Sex = "boy" };//数据源
            this.DataContext = this.stu;//前台绑定代码

            //后台绑定方式(数据双向绑定，UI的变化会引起数据变化，数据变化也会引起UI变化)
            ////姓名
            //Binding stunamebd = new Binding();//定义绑定
            //stunamebd.Source = this.stu;//绑定数据源
            //stunamebd.Path = new PropertyPath("Name");//绑定路径
            ////性别
            //Binding stusexbd = new Binding();//定义绑定
            //stusexbd.Source = this.stu;//绑定数据源
            //stusexbd.Path = new PropertyPath("Sex");//绑定路径
            ////年龄
            //Binding stuagebd = new Binding();//定义绑定
            //stuagebd.Source = this.stu;//绑定数据源
            //stuagebd.Path = new PropertyPath("Age");//绑定路径

            //this.txtname.SetBinding(TextBox.TextProperty, stunamebd);//关联姓名对象
            //this.txtsex.SetBinding(TextBox.TextProperty, stusexbd);//关联性别对象
            //this.txtage.SetBinding(TextBox.TextProperty, stuagebd);//关联年龄对象


            //前台绑定方式:1.数据源(与后台绑定相同)；2.定义绑定路径；3.定义绑定方向(双向还是单向)；
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int num = new Random().Next(100);
            this.stu.Name = "张三" + num;
            this.stu.Sex = num % 2 == 0 ? "Boy" : "Girl";
            this.stu.Age = num;
        }
    }
}
